package org.csu.onlineedubackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.csu.onlineedubackend.dto.ForumNotificationDTO;
import org.csu.onlineedubackend.entity.ForumNotification;
import org.csu.onlineedubackend.entity.User;
import org.csu.onlineedubackend.mapper.ForumNotificationMapper;
import org.csu.onlineedubackend.mapper.UserMapper;
import org.csu.onlineedubackend.service.ForumNotificationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 論壇通知服務實現類
 */
@Service
public class ForumNotificationServiceImpl implements ForumNotificationService {

    @Autowired
    private ForumNotificationMapper forumNotificationMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean createNotification(ForumNotification notification) {
        return forumNotificationMapper.insert(notification) > 0;
    }

    @Override
    public List<ForumNotification> getNotificationsByUser(Integer userId) {
        LambdaQueryWrapper<ForumNotification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ForumNotification::getUserId, userId)
               .orderByDesc(ForumNotification::getCreatedAt);
        return forumNotificationMapper.selectList(wrapper);
    }

    @Override
    public int countUnread(Integer userId) {
        LambdaQueryWrapper<ForumNotification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ForumNotification::getUserId, userId)
               .eq(ForumNotification::getIsRead, 0);
        return forumNotificationMapper.selectCount(wrapper).intValue();
    }

    @Override
    public boolean markAsRead(Integer notificationId, Integer userId) {
        ForumNotification notification = forumNotificationMapper.selectById(notificationId);
        if (notification == null || !notification.getUserId().equals(userId)) {
            return false;
        }
        notification.setIsRead(1);
        return forumNotificationMapper.updateById(notification) > 0;
    }

    @Override
    public boolean markAllAsRead(Integer userId) {
        LambdaQueryWrapper<ForumNotification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ForumNotification::getUserId, userId)
               .eq(ForumNotification::getIsRead, 0);
        ForumNotification update = new ForumNotification();
        update.setIsRead(1);
        return forumNotificationMapper.update(update, wrapper) > 0;
    }

    // 新增：刪除單條通知
    @Override
    public boolean deleteNotification(Integer notificationId) {
        return forumNotificationMapper.deleteById(notificationId) > 0;
    }

    // 新增：刪除用戶全部通知
    @Override
    public boolean deleteAllNotifications(Integer userId) {
        LambdaQueryWrapper<ForumNotification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ForumNotification::getUserId, userId);
        return forumNotificationMapper.delete(wrapper) > 0;
    }

    // 其他輔助方法可按需添加 (如 DTO 轉換)
} 